
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="商品名称:" prop="goodsName">
          <el-input v-model="formData.goodsName" :clearable="true"  placeholder="请输入商品名称" />
       </el-form-item>
        <el-form-item label="商品简介:" prop="goodsIntroduction">
          <el-input v-model="formData.goodsIntroduction" :clearable="true"  placeholder="请输入商品简介" />
       </el-form-item>
        <el-form-item label="详细描述:" prop="goodsIntro">
          <el-input v-model="formData.goodsIntro" :clearable="true"  placeholder="请输入详细描述" />
       </el-form-item>
        <el-form-item label="商品分类:" prop="goodsType">
          <el-input v-model="formData.goodsType" :clearable="true"  placeholder="请输入商品分类" />
       </el-form-item>
        <el-form-item label="单位:" prop="goodsUint">
           <el-select v-model="formData.goodsUint" placeholder="请选择单位" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in goodsUnitOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="重量:" prop="goodsWeight">
          <el-input v-model="formData.goodsWeight" :clearable="true"  placeholder="请输入重量" />
       </el-form-item>
        <el-form-item label="库存:" prop="goodsInventory">
          <el-input v-model.number="formData.goodsInventory" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="库存不足数量:" prop="goodsInventorys">
          <el-input v-model.number="formData.goodsInventorys" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="商品价格:" prop="goodsPrice">
          <el-input-number v-model="formData.goodsPrice" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="是否上架:" prop="isSale">
           <el-select v-model="formData.isSale" placeholder="请选择是否上架" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in isSaleOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="商品图片:" prop="goodsImg">
          <el-input v-model="formData.goodsImg" :clearable="true"  placeholder="请输入商品图片" />
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createLsGood,
  updateLsGood,
  findLsGood
} from '@/api/good/lsGood'

defineOptions({
    name: 'LsGoodForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const goodsUnitOptions = ref([])
const isSaleOptions = ref([])
const formData = ref({
            goodsName: '',
            goodsIntroduction: '',
            goodsIntro: '',
            goodsType: '',
            goodsUint: '',
            goodsWeight: '',
            goodsInventory: undefined,
            goodsInventorys: undefined,
            goodsPrice: 0,
            isSale: '',
            goodsImg: '',
        })
// 验证规则
const rule = reactive({
               goodsName : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodsIntro : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodsType : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodsUint : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodsInventory : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodsPrice : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               isSale : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               goodsImg : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findLsGood({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    goodsUnitOptions.value = await getDictFunc('goodsUnit')
    isSaleOptions.value = await getDictFunc('isSale')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createLsGood(formData.value)
               break
             case 'update':
               res = await updateLsGood(formData.value)
               break
             default:
               res = await createLsGood(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
